/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server.gui;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.text.*;

import server.TestingServer;
import shared.Packet3StyleChat;

/**
 *
 * @author Mooman
 */
public class ServerGUI extends javax.swing.JFrame {
    private static final long serialVersionUID = 1L;
    TestingServer host;
    /**
     * Creates new form ServerGUI
     */
    public ServerGUI(TestingServer host) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ServerGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        initComponents();
        this.host = host;
        this.setTitle("Server");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        inputField = new JTextField();
        mainOutput = new JTextPane();
        userListScroll = new JScrollPane();
        userList = new JTextArea();
        mainDoc = mainOutput.getStyledDocument();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        requestFocus(false);

        inputField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                inputFieldKeyPressed(evt);
            }
        });

        mainOutput.setEditable(false);
        mainOutput.setPreferredSize(new java.awt.Dimension(370, 430));
        mainOutputScroll = new JScrollPane(mainOutput);
        mainOutput.getAccessibleContext().setAccessibleDescription("");
        mainOutputScroll.setPreferredSize(new java.awt.Dimension(370, 430));
        Style y = mainOutput.addStyle("Base", null);
        StyleConstants.setFontSize(y, 16);
        StyleConstants.setBold(y, true);
        StyleConstants.setForeground(y, Color.gray);
        y = mainOutput.addStyle("Server", null);
        StyleConstants.setFontSize(y, 16);
        StyleConstants.setBold(y, true);
        StyleConstants.setForeground(y, Color.black);
        

        userListScroll.setPreferredSize(new java.awt.Dimension(100, 100));

        userList.setColumns(11);
        userList.setEditable(false);
        userList.setRows(5);
        userListScroll.setViewportView(userList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(mainOutputScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(userListScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))
                                        .addComponent(inputField))
                                        .addContainerGap())
                );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(userListScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
                                .addComponent(mainOutputScroll, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(inputField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
                );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void inputFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_inputFieldKeyPressed
        if(evt.getKeyCode() != KeyEvent.VK_ENTER) return;
        String message = inputField.getText();
        inputField.setText("");
        host.packetHandler.sendGlobalPacket(new Packet3StyleChat("Server", message, mainOutput.getStyle("Server")));
        write("Server: ", mainOutput.getStyle("Base"));
        addLine(message);
    }//GEN-LAST:event_inputFieldKeyPressed


    public void addLine(String line){
        write(line+"\n", null);
    }

    public void updateUserList(ArrayList<String> names){
        userList.setText("");
        for(String name : names)
            userList.append(name + "\n");
    }

    public void write(String s, Style y){
        try {
            mainDoc.insertString(mainDoc.getLength(), s, y);
            mainOutput.select(mainOutput.getText().length()-1, mainOutput.getText().length());
        } catch (BadLocationException e) {}
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JTextField inputField;
    public JTextPane mainOutput;
    private StyledDocument mainDoc;
    private JScrollPane mainOutputScroll;
    private JTextArea userList;
    private JScrollPane userListScroll;
    // End of variables declaration//GEN-END:variables
}
